<?php
/**
 * 商品分类Widget
 * 用于调用商品分类
 * @version 2014122411
 * @author Max.Yu <max@jipu.com>
 */
 
namespace Home\Widget;

use Home\Model\ItemCategoryModel;

class ItemCategoryWidget extends BaseWidget{

  /**
   * 调用分类树导航菜单
   * @author Max.Yu <max@jipu.com>
   */
  public function category($params = array()){
    //实例化数据模型
    $item_category_model = D('ItemCategory');

    //$where = $params['where']?$params['where']:array();
    $field = $params['fields']?$params['fields']:true;
    $order = $params['order']?$params['order']:'`sort` ASC, `id` DESC';
    $limit = $params['limit']?$params['limit']:10;
    //获取商品分类数据
    $where = array(
          'pid' => array('eq',0),
          'is_display'=>1,
          'status'=>1,
          'is_topshow'=>1,
          'trait'=>array('like','%,2,%')
      );
    $list  = $item_category_model->lists($where,$field,$order,$limit);
    //模板输出变量赋值
    $this->list = $list;
    if($params['display']){
      $this->display($params['display']);
    }else{
      $this->display('Widget/itemCategory');
    }
  }

  public function cate_new($params = array()){
    $data  = S('cate_new');
    if( empty($data) ){
      // 顶级分类显示条件 ：置顶、显示
      $data  = M('ItemCategory')->where('is_display = 1 and status = 1 and pid =0 and is_topshow = 1 ')->order('sort asc')->field('name,id,hotwords')->select();
      foreach($data as $k => $v){
        //二级分类显示条件：显示、置顶、数目（2）
        $data[$k]['list'] = M("ItemCategory")->where('trees like ",'.$v['id'].',%" and is_display = 1 and status  = 1 and is_topshow = 1')->order('sort asc,id asc')->field('name,id,pid')->select();
        //去掉禁用的 start
        foreach ($data[$k]['list'] as $key => $value) {
          $_value = M('ItemCategory')->where('id = '.$value['pid'])->getField('status');
          if ($_value != 1) {
            unset($data[$k]['list'][$key]);
          }
        }
        $data[$k]['list'] = array_values($data[$k]['list']);
        //去掉禁用的 end

        //右侧分类显示条件：首页模块、显示
        $data[$k]['cate'] = M("ItemCategory")->where('trees like ",'.$v['id'].',%" and is_display = 1 and status  = 1')->order('sort asc')->limit()->field('name,id,pid')->select();
        foreach ($data[$k]['cate'] as $key => $value) {
          //去掉禁用的 start
          $_value = M('ItemCategory')->where('id = '.$value['pid'])->getField('status');
          if ($_value != 1) {
            unset($data[$k]['cate'][$key]);
          }
        }
        $data[$k]['cate'] = array_values($data[$k]['cate']);
        //去掉禁用的 end
        
        // 右侧文字品牌显示条件：推荐 数目（10）
        $data[$k]['brand'] = M('Brand')->where('cid='.$v['id'].' and status= 1')->limit(10)->order('sort asc')->select();
        // 右侧关键字显示条件：顶级分类显示关键词
        $data[$k]['hotwords']    = array_filter(explode('|', $v['hotwords']));
        // 右侧图片品牌显示条件： 热门 数目(10)
        $data[$k]['hot_brand'] = M('Brand')->where('cid='.$v['id'].' and status= 1')->limit(10)->field('pic,id')->select();
      }
      foreach ($data as $kv => $val) {
        if ( (empty($val['hotwords'])) && (empty($val['list'])) && (empty($val['cate'])) && (empty($val['brand'])) && (empty($val['hot_brand']))) {
          unset($data[$kv]);
        }
      }
      $data = array_values($data);
      //dump($data);
      S('cate_new' , $data) ;
    }
    
    $this->data = $data ;
    $this->display('Widget/cate_new');
  }
}